﻿using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using TinyContactManager.Web.Internal.Entities;

namespace TinyContactManager.Web.Models
{
    public class RegistrationModel
    {
        public RegistrationModel()
        {
        }

        public RegistrationModel(Contact contact)
        {
            Id = contact.Id;
            FirstName = contact.FirstName;
            LastName = contact.LastName;
            Birthday = contact.DateOfBirth.ToString("yyyy-MM-dd");
            Phone = contact.Telephone;
            Email = contact.Email;
            if (contact.PostalAddress != null)
            {
                Postcode = contact.PostalAddress.Postcode;
                State = contact.PostalAddress.State;
                Street1 = contact.PostalAddress.Street1;
                Street2 = contact.PostalAddress.Street2;
                Suburb = contact.PostalAddress.Suburb;
            }
            if (contact.Mailings != null)
            {
                var mailing = contact.Mailings.OrderByDescending(x => x.Date).FirstOrDefault(x => x.Date > DateTime.Today.AddMonths(-12));
                if (mailing != null)
                {
                    HasBeenSentVoucherThisYear = mailing.Date;
                    HasRedeemedVoucher = mailing.Responded;
                }
            }
        }

        public DateTime? HasRedeemedVoucher { get; set; }
        public DateTime? HasBeenSentVoucherThisYear { get; set; }

        public int Id { get; set; }
        [Required] public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Birthday { get; set; }
        public string Phone { get; set; }
        [Required]
        public string Street1 { get; set; }
        public string Street2 { get; set; }
        [Required]
        public string Suburb { get; set; }
        [Required]
        public string Postcode { get; set; }
        [Required]
        public string State { get; set; }
        public string Email { get; set; }
    }
}